import { Component, OnInit, OnChanges, Input, ViewChild, SimpleChange } from '@angular/core';
import { TaskListService } from './tasklist.service';
import { CommandDispatcherService } from './menus/command-dispatcher-service';
import { DialogService } from 'primeng/primeng';
import { TaskListMenuComponent } from './menus/tasklist-menu.component';
@Component({
    selector: 'tasklist',
    templateUrl: './tasklist.component.html',
    styleUrls: ['./tasklist.component.scss'],
    providers: [TaskListService, CommandDispatcherService]
})
export class TaskListComponent implements OnInit, OnChanges {

    @Input()
    systemUrl: string = '';
    @Input()
    taskListGroupId: number = 0;
    @Input()
    bizId: number = 0;

    loading: boolean = true;
    //栏目分组

    tableStyle: any;
    //导航栏
    tasklists: any[] = [];
    //查询条件
    searchFields: any[] = [];
    // 列
    cols: any[] = [];
    //当页记录
    records: any[] = [];
    //菜单操作项
    menus: any[] = [];
    //详情列表
    details: any[] = [];
    totalRecords: number = 0;
    showPaginator: boolean = false;
    scrollHeight: string = '140px';
    isDisplay: boolean = false;

    selectedRecord: any;
    taskListId: number;
    recId: any;

    pageSize: number = 15;

    sortPageInfo: any = {};//表头排序分页
    self: any;
    callback: any;
    @ViewChild(TaskListMenuComponent, { static: false })
    private taskListMenuComponent: TaskListMenuComponent;

    constructor(
        private dialogService: DialogService,
        private taskListService: TaskListService,
        private commandDispatcherService: CommandDispatcherService) {
        this.self = this;
        this.callback = this.searchTaskListData;
    }

    ngOnInit() {
        this.loading = true;
        this.taskListService.getTaskListDetailTabs(this.taskListGroupId, this.systemUrl).subscribe(data => {
            this.tasklists = data;
            this.selectedRecord = null;
            if (this.tasklists && this.tasklists.length > 0) {
                this.taskListId = this.tasklists[0]['taskListId'];
                if (this.taskListId) {
                    this.loadTaskListMenu(this.taskListId, 0, 0);
                    this.loadTaskListSearchFields(this.taskListId);
                    this.loadTaskListFields(this.taskListId);
                }
            } else {
                this.menus = [];
                this.searchFields = [];
                this.cols = [];
                this.loading = false;
            }
        })
        window.onresize = () => this.scrollHeight = $(document).height() - $(".trans-tasklist-fields").height() - 200 + 'px';
    }

    /** 栏目切换 */
    onTaskListTabChange(event) {
        let tasklist = this.tasklists[event.index];
        this.taskListId = tasklist.taskListId;
        this.loadTaskListMenu(this.taskListId, 0, 0);
        this.loadTaskListSearchFields(this.taskListId);
        this.loadTaskListFields(this.taskListId);
    }

    /** 点击操作项 */
    dispatcherCommand(event, command) {
        this.commandDispatcherService.dispatcher(
            command, //点击指令
            this.selectedRecord, //当前选中
            this,  //当前实例
            this.searchTaskListData //回调方法
        );
    }

    /** 分页加载 */
    loadLazy(event: any) {
        // 分页排序参数
        this.sortPageInfo = {};
        if (event['sortField']) {
            this.sortPageInfo['sortField'] = event['sortField'];
            this.sortPageInfo['sortType'] = event['sortOrder'] > 0 ? 'asc' : 'desc';
        }
        if (event.rows) {
            this.sortPageInfo['pageSize'] = 15;
            this.sortPageInfo['pageNum'] = event.first / event.rows + 1;
        }
        this.searchTaskListData(this.sortPageInfo);
    }

    /** 行选中 */
    onRowSelect(event) {
        this.recId = event.data.rec_id;
        this.loadTaskListMenu(this.taskListId, 
            this.selectedRecord[0]['act_def_id'] || 0,
            this.selectedRecord[0]['act_id'] || 0);
    }

    //加载菜单
    loadTaskListMenu(taskListId, actDefId, actId) {
        this.taskListMenuComponent.loadTaskListMenu(taskListId, actDefId, 1, actId, this.systemUrl);
    }
    //加载表头
    loadTaskListFields(taskListId) {
        this.taskListService.loadTaskListFields(taskListId, this.systemUrl).subscribe(fields => {
            this.cols = [];
            fields.forEach(header => 1 == header.visibleFlag && this.cols.push(header));
            this.searchTaskListData({});
        });
    }
    //加载查询条件
    loadTaskListSearchFields(taskListId) {
        this.taskListService.loadTaskListSearchFields(this.taskListId, this.systemUrl).subscribe(searchFields => {
            let search = [];
            searchFields.forEach(ele => search.push(ele));
            this.searchFields = search;
        });
    }
    //加载当前页数据
    searchTaskListData(pageSortInfo) {
        this.isDisplay = false;
        this.selectedRecord = null;
        this.loading = true;
        if (!this.taskListId) return;
        let params = { 'taskListId': this.taskListId, 'searchFields': this.searchFields };
        if (pageSortInfo) $.extend(params, pageSortInfo);
        this.taskListService.searchTaskListData(params, this.systemUrl).subscribe(page => {
            if (page.list && page.list[0] && page.list[0].rec_type_id == '02') {
                this.isDisplay = true;
            }
            this.loading = false;
            this.totalRecords = page.total;
            this.pageSize = page.pageSize;
            this.records = page.list;
            this.showPaginator = (page.total > 15);
            let condHeight = $(".trans-tasklist-fields").height();
            this.scrollHeight = ($(document).height() - condHeight - 200) + 'px';
            if (this.totalRecords > 0) {
                this.selectedRecord = [this.records[0]];
                this.recId = this.selectedRecord[0]['rec_id'];
                this.loadTaskListMenu(this.taskListId, 
                    this.selectedRecord[0]['act_def_id'] || 0,
                    this.selectedRecord[0]['act_id'] || 0);
            } else {
                this.menus = [];
                this.loadTaskListMenu(this.taskListId, 0, 0);
            }
        });
    }
    //重置
    reset() {
        this.searchFields.forEach(ele => ele['value'] = '');
    }
    customSort() { }

    //监听tasklistgroupid的变化
    ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
        for (let propName in changes) {
            if(propName == 'taskListGroupId'){
                this.ngOnInit();
            }
        }
    }
}
